from ..original.shipment_api import ShipmentAPI
import json


class Shipment(object):

    @classmethod
    def address(
        cls,
        name: str,
        company_name: str,
        country: str,
        state: str,
        city: str,
        street: str,
        postal_code: str,
        phone: str,
        email: str,
    ) -> dict:
        """
        封装地址
        :param name: 姓名
        :param company_name:  公司名称
        :param country: 国家
        :param state: 省份
        :param city: 城市
        :param street: 街道
        :param postal_code: 邮编
        :param phone: 电话
        :param email: 邮箱
        """
        return {
            "name": name if name else "mact",
            "company_name": company_name if company_name else "palm",
            "street1": street if street else "1",
            "street2": "1",
            "city": city if city else "",
            "state": state if state else "",
            "postal_code": postal_code if postal_code else "",
            "country": country if country else "",
            "phone": phone if phone else "1",
            "email": email if email else "chaoji000010@gmail.com",
        }

    @classmethod
    def parcels(
        cls,
        parcels_list: list,
        desc: str,
        value: float,
        currency="EUR",
    ) -> dict:
        """
        封装包裹
        :param parcels_list: 包裹内容明细，内容与封装结构：
               [{"length": 5,"width": 5,"height": 5,"weight": 10,"parcel_id":"123","parcel_type":"Box"},...]
               parcel_id,parcel_type 不是必填
        :param value: 包裹价值
        :param currency: 货币单位(默认EUR)
        :param desc: 包裹描述
        """
        parcels = []
        for p in parcels_list:
            p_line = {
                "length": p["length"],
                "width": p["width"],
                "height": p["height"],
                "weight": p["weight"],
                "Attributes": [
                    {"name": "parcelID", "value": p["parcel_id"]},
                    {"name": "parcelType", "value": p["parcel_type"]},
                ],
            }
            parcels.append(p_line)
        return {"parcels": parcels, "desc": desc, "value": value, "currency": currency}

    @classmethod
    def insurance(cls, price: float, currency="EUR"):
        """
        封装保险
        :param price 保险价格
        :param currency 货币单位(默认EUR)
        """
        return {"price": price, "currency": currency}

    @classmethod
    def courier_service(
        cls, courier: str, service: str, shippypro_code: int, courier_options: list
    ):
        """
        封装承运商服务
        :param courier: 承运商名称
        :param service: 承运商服务名称
        :param shippypro_code: shippypro定义承运商服务的编码
        :param courier_options: 承运商增值服务[{"name": " ","value": "0"},...]
        """
        return {
            "courier_name": courier,
            "courier_service_name": service,
            "courier_service_code": shippypro_code,
            "courier_options": courier_options,
        }

    @classmethod
    def getRates(
        cls,
        receive_address: dict,
        send_address: dict,
        parcels: dict,
        insurance: dict,
        # courier_service: dict,
        # transaction_code: str,
        # order_code="",
        # payment_method="",
    ):
        """ """
        return ShipmentAPI.getRates(
            receive_name=receive_address["name"],
            receive_company_name=receive_address["company_name"],
            receive_street=receive_address["street1"],
            receive_city=receive_address["city"],
            receive_state=receive_address["state"],
            receive_postal_code=receive_address["postal_code"],
            receive_country=receive_address["country"],
            receive_phone=receive_address["phone"],
            receive_email=receive_address["email"],
            send_name=send_address["name"],
            send_company_name=send_address["company_name"],
            send_street=send_address["street1"],
            send_city=send_address["city"],
            send_state=send_address["state"],
            send_postal_code=send_address["postal_code"],
            send_country=send_address["country"],
            send_phone=send_address["phone"],
            send_email=send_address["email"],
            parcels_list=parcels["parcels"],
            parcel_value=str(parcels["value"]) + " " + parcels["currency"],
            parcel_desc=parcels["desc"],
            insurance_price=0,
            insurance_currency="EUR",
        )

    @classmethod
    def create_ship(
        cls,
        receive_address: dict,
        send_address: dict,
        parcels: dict,
        insurance: dict,
        courier_service: dict,
        transaction_code: str,
        order_code="",
        payment_method="",
    ):
        """
        创建运单
        :param receive_address: 收件人地址{"name": "","company": "","street1": "","street2": "","city": "","state": "","zip": "","country": "","phone": "","email": ""}
        :param send_address:  发件人地址{"name": "","company": "","street1": "","street2": "","city": "","state": "","zip": "","country": "","phone": "","email": ""}
        :param parcels: 包裹信息{'parcels': [], 'desc': "", 'value': "", 'currency': ""}
        :param insurance: 保险信息{'price': '', 'currency': ''}
        :param courier_service: 承运商服务信息{"courier_name": '','courier_service_name': '','courier_service_code': '','courier_options':[]}
        :param transaction_code: 交易订单编码
        :param order_code: 订单编码,用于获取打印的面单
        :param payment_method: 支付方式
        """
        return ShipmentAPI.ship(
            receive_name=receive_address["name"],
            receive_company_name=receive_address["company_name"],
            receive_street=receive_address["street1"],
            receive_city=receive_address["city"],
            receive_state=receive_address["state"],
            receive_postal_code=receive_address["postal_code"],
            receive_country=receive_address["country"],
            receive_phone=receive_address["phone"],
            receive_email=receive_address["email"],
            send_name=send_address["name"],
            send_company_name=send_address["company_name"],
            send_street=send_address["street1"],
            send_city=send_address["city"],
            send_state=send_address["state"],
            send_postal_code=send_address["postal_code"],
            send_country=send_address["country"],
            send_phone=send_address["phone"],
            send_email=send_address["email"],
            parcels_list=parcels["parcels"],
            parcel_value=str(parcels["value"]) + " " + parcels["currency"],
            parcel_desc=parcels["desc"],
            insurance_price=insurance["price"],
            insurance_currency=insurance["currency"],
            courier_name=courier_service["courier_name"],
            courier_service_name=courier_service["courier_service_name"],
            courier_service_code=courier_service["courier_service_code"],
            courier_options=courier_service["courier_options"],
            transaction_code=transaction_code,
            order_code=order_code,
            payment_method=payment_method,
        )
        #     flag = response.get('Result')
        #     if flag == 'OK':
        #         error_message = response.get('ErrorMessage', False)
        #         if not error_message:
        #             return True, response
        #         else:
        #             return False, error_message
        #     else:
        #         return False, "连接失败，失败原因：{}".format(response['Error'])
        # except Exception as e:
        #     return False, '调用接口失败，失败原因：{}'.format(e)
